package servlet;

import dao.UserDao;
import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/GoodsUserServlet.do")
public class GoodsUserServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println(username);
        // 使用UserDao根据username获取user_id
        UserDao userDao = new UserDao();



            try {
                int user_id =  userDao.getUserIdByUsername(username);

                List<Map<String, Object>> searchResults1 = performQueryById("SELECT * FROM goods WHERE user_id = ?", user_id);

                session.setAttribute("searchResults1", searchResults1);

                response.sendRedirect(request.getContextPath() + "/background1/user/goodsManager.jsp");
            } catch (NumberFormatException e) {
                // Handle the case where user_idString is not a valid integer
                e.printStackTrace(); // Replace with appropriate error handling
            }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    private List<Map<String, Object>> performQueryById(String sql, int user_id) {
        List<Map<String, Object>> goodsList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = null;

        try {
            resultSet = jdbcUtil.prepareQuery(sql, user_id);

            while (resultSet != null && resultSet.next()) {
                int goods_id = resultSet.getInt("goods_id");
                String created_at = resultSet.getString("created_at");
                String goods_image = resultSet.getString("goods_image");
                String goods_info = resultSet.getString("goods_info");
                String goods_category = resultSet.getString("goods_category");
                double goods_price = resultSet.getDouble("goods_price");

                Map<String, Object> goods = new HashMap<>();
                goods.put("goods_id", goods_id);
                goods.put("created_at", created_at);
                goods.put("goods_image", goods_image);
                goods.put("goods_info", goods_info);
                goods.put("goods_category", goods_category);
                goods.put("goods_price", goods_price);

                goodsList.add(goods);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return goodsList;
    }
}
